package excercise.recursive;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合的输出
 * String s = "abcdef"; 输入n表示子集的长度
 */
public class Question1_3 {
    public List<String> solution(String str, int n) {
        char[] arr = str.toCharArray();
        List<String> r = new ArrayList<>();
        List<String> path = new ArrayList<>();
        f(0, arr, n, path, r);

        r.forEach(System.out::println);

        return r;
    }

    public void f(int curIndex, char[] arr, int n, List<String> path, List<String> r) {

        if (path.size() == n) {
            r.add(getPath(path));
            return;
        }

        for (int i = curIndex; i < arr.length; i++) {
            path.add(String.valueOf(arr[i]));
            f(i + 1, arr, n, path, r);
            path.remove(path.size() - 1);
        }

    }

    public String getPath(List<String> path) {
        StringBuilder s = new StringBuilder();
        for (String value : path) {
            s.append(value);
        }
        return s.toString();
    }

    public static void main(String[] args) {
        new Question1_3().solution("abcde", 3);
    }
}
